if SERVER then

/************************************************************************/
/* TABLES CREATION FUNCTIONS											*/
/************************************************************************/

/* PlayerInfo */
function CreatePlayerInfoTable()

	/* Check if table already exists */
	if sql.TableExists( "PlayerInfo" ) then
		DEBUG:Msg( "Skipped creation of 'PlayerInfo' because it already exists", "CreatePlayerInfoTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE PlayerInfo ( "
	query = query	.. "SteamID varchar(255) PRIMARY KEY, "
	query = query	.. "FirstJoin int, "
	query = query	.. "LastJoin int, "
	query = query	.. "Money float )"
	
	/* Debug output */
	DEBUG:Msg( "Creating PlayerInfo table ...", "CreatePlayerInfoTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* Loadout */
function CreateLoadoutTable()

	/* Check if table already exists */
	if sql.TableExists( "Loadout" ) then
		DEBUG:Msg( "Skipped creation of 'Loadout' because it already exists", "CreateLoadoutTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE Loadout ( "
	query = query	.. "ID int PRIMARY KEY, "
	query = query	.. "Name varchar(255), "
	query = query	.. "PrimaryWeapon int, "
	query = query	.. "SecondaryWeapon int, "
	query = query	.. "Inventory1 int, "
	query = query	.. "Inventory2 int, "
	query = query	.. "Backpack1 int, "
	query = query	.. "Backpack2 int )"
	
	/* Debug output */
	DEBUG:Msg( "Creating Loadout table ...", "CreateLoadoutTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* TeamInfo */
function CreateTeamInfoTable()

	/* Check if table already exists */
	if sql.TableExists( "TeamInfo" ) then
		DEBUG:Msg( "Skipped creation of 'TeamInfo' because it already exists", "CreateTeamInfoTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE TeamInfo ( "
	query = query	.. "ID int PRIMARY KEY, "
	query = query	.. "Name varchar(255), "
	query = query	.. "Color varchar(50), "
	query = query	.. "MaxPercent int, "
	query = query	.. "ChatName varchar(255), "
	query = query	.. "DefaultLoadout int, "
	query = query	.. "CreateTime int )"
	
	/* Debug output */
	DEBUG:Msg( "Creating TeamInfo table ...", "CreateTeamInfoTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* ObjectType: turret / barricade / weapon */
function CreateObjectTypeTable()

	/* Check if table already exists */
	if sql.TableExists( "ObjectType" ) then
		DEBUG:Msg( "Skipped creation of 'ObjectType' because it already exists", "CreateObjectTypeTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE ObjectType ( "
	query = query	.. "ID int PRIMARY KEY, "
	query = query	.. "Name varchar(255), "
	query = query	.. "ClassName varchar(255) )"
	
	/* Debug output */
	DEBUG:Msg( "Creating ObjectType table ...", "CreateObjectTypeTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* ObjRefType: entity / weapon / contraption */
function CreateObjRefTypeTable()

	/* Check if table already exists */
	if sql.TableExists( "ObjRefType" ) then
		DEBUG:Msg( "Skipped creation of 'ObjRefType' because it already exists", "CreateObjRefTypeTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE ObjRefType ( "
	query = query	.. "ID int PRIMARY KEY, "
	query = query	.. "Name varchar(255) )"
	
	/* Debug output */
	DEBUG:Msg( "Creating ObjRefType table ...", "CreateObjRefTypeTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* LevelInfo */
function CreateLevelInfoTable()

	/* Check if table already exists */
	if sql.TableExists( "LevelInfo" ) then
		DEBUG:Msg( "Skipped creation of 'LevelInfo' because it already exists", "CreateLevelInfoTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE LevelInfo ( "
	query = query	.. "Num int PRIMARY KEY, "
	query = query	.. "RequiredXp int )"
	
	/* Debug output */
	DEBUG:Msg( "Creating LevelInfo table ...", "CreateLevelInfoTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* ObjectInfo */
function CreateObjectInfoTable()

	/* Check if table already exists */
	if sql.TableExists( "ObjectInfo" ) then
		DEBUG:Msg( "Skipped creation of 'ObjectInfo' because it already exists", "CreateObjectInfoTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE ObjectInfo ( "
	query = query	.. "ID int PRIMARY KEY, "
	query = query	.. "Name varchar(255), "
	query = query	.. "Health int, "
	query = query	.. "Weight int, "
	query = query	.. "Type int, "
	query = query	.. "Reference varchar(255), "
	query = query	.. "ReferenceType int, "
	query = query	.. "Price float )"
	
	/* Debug output */
	DEBUG:Msg( "Creating ObjectInfo table ...", "CreateObjectInfoTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* UpgradeInfo */
function CreateUpgradeInfoTable()

	/* Check if table already exists */
	if sql.TableExists( "UpgradeInfo" ) then
		DEBUG:Msg( "Skipped creation of 'UpgradeInfo' because it already exists", "CreateUpgradeInfoTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE UpgradeInfo ( "
	query = query	.. "ID int PRIMARY KEY, "
	query = query	.. "Name varchar(255), "
	query = query	.. "Level int, "
	query = query	.. "AddHealth int, "
	query = query	.. "AddWeight int, "
	query = query	.. "Price float, "
	query = query	.. "Object int, "
	query = query	.. "Previous int,"
	query = query	.. "Next int )"
	
	/* Debug output */
	DEBUG:Msg( "Creating UpgradeInfo table ...", "CreateUpgradeInfoTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* PlayerStats */
function CreatePlayerStatsTable()

	/* Check if table already exists */
	if sql.TableExists( "PlayerStats" ) then
		DEBUG:Msg( "Skipped creation of 'PlayerStats' because it already exists", "CreatePlayerStatsTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE PlayerStats ( "
	query = query	.. "SteamID varchar(255), "
	query = query	.. "TeamID int, "
	query = query	.. "Kills int, "
	query = query	.. "Deaths int, "
	query = query	.. "Rescued int, "
	query = query	.. "Escaped int, "
	query = query	.. "FriendsKilled int, "
	query = query	.. "EnemiesRescued int, "
	query = query	.. "Experience int, "
	query = query	.. "Level int,"
	query = query	.. "CONSTRAINT PK_PlayerStats PRIMARY KEY ( SteamID, TeamID ) )"
	
	/* Debug output */
	DEBUG:Msg( "Creating PlayerStats table ...", "CreatePlayerStatsTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* PlayerUpgrade */
function CreatePlayerUpgradeTable()

	/* Check if table already exists */
	if sql.TableExists( "PlayerUpgrade" ) then
		DEBUG:Msg( "Skipped creation of 'PlayerUpgrade' because it already exists", "CreatePlayerUpgradeTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE PlayerUpgrade ( "
	query = query	.. "SteamID varchar(255), "
	query = query	.. "Upgrade int, "
	query = query	.. "DateAcquired int, "
	query = query	.. "CONSTRAINT PK_PlayerUpgrade PRIMARY KEY ( SteamID, Upgrade ) )"
	
	/* Debug output */
	DEBUG:Msg( "Creating PlayerUpgrade table ...", "CreatePlayerUpgradeTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/* UserInfo */
function CreateUserInfoTable()

	/* Check if table already exists */
	if sql.TableExists( "UserInfo" ) then
		DEBUG:Msg( "Skipped creation of 'UserInfo' because it already exists", "CreateUserInfoTable" )
		return true
	end
	
	/* Prepare the query */
	local query		= "CREATE TABLE UserInfo ( "
	query = query	.. "SteamID varchar(255) PRIMARY KEY, "
	query = query	.. "Login varchar(255), "
	query = query	.. "Password varchar(255), "
	query = query	.. "AdminLevel int )"
	
	/* Debug output */
	DEBUG:Msg( "Creating UserInfo table ...", "CreateUserInfoTable" )
	
	/* Execute the query */
	return sql.Query( query )
end

/************************************************************************/
/* GAMEMODE FUNCTIONS													*/
/************************************************************************/
GM.CreateDataBase = function()
	
	/* Variables */
	local result = true
	
	/* Create the tables */
	result = result && ( CreatePlayerInfoTable() != false )
	result = result && ( CreateObjectTypeTable() != false )
	result = result && ( CreateObjRefTypeTable() != false )
	result = result && ( CreateLevelInfoTable() != false )
	result = result && ( CreateObjectInfoTable() != false )
	result = result && ( CreateLoadoutTable() != false )
	result = result && ( CreateTeamInfoTable() != false )
	result = result && ( CreateUpgradeInfoTable() != false )
	result = result && ( CreatePlayerStatsTable() != false )
	result = result && ( CreatePlayerUpgradeTable() != false )
	result = result && ( CreateUserInfoTable() != false )
	
	/* Debug output - error */
	if not result then
		DEBUG:ErrMsg( "Error while trying to create the database. Last error: " .. sql.LastError(), "GM.CreateDataBase" )
		
	/* Debug output - no error */
	else
		DEBUG:Msg( "The database tables have been successfully created !", "GM.CreateDataBase" )
	end
end

/************************************************************************/
/* TESTS																*/
/************************************************************************/
function TestDB()

	Msg( "----------\nSTART DB TEST\n" )

	local steam0 = SQLStr( "STEAM_0:0:0" )
	local steam1 = SQLStr( "STEAM_0:0:1" )
	sql.Query( "CREATE TABLE TestDB ( SteamID varchar(255) PRIMARY KEY, Money float, Name varchar(10), FirstTime int )" )
	sql.Query( "INSERT INTO TestDB ( 'SteamID', 'Money', 'Name', 'FirstTime' ) VALUES ( " .. steam0 .. ", 10, 'abcdefghij', " .. tostring( os.time() ) .. " ) " )
	sql.Query( "INSERT INTO TestDB ( 'SteamID', 'Money', 'Name', 'FirstTime' ) VALUES ( " .. steam1 .. ", 50.5, 'greg', " .. tostring( os.time() ) .. " ) " )
	local result = sql.Query( "SELECT * FROM TestDB" )
	if result then
		for id,row in pairs( result ) do
			local text = tostring( id) .. ":"
			for col,val in pairs( row ) do
				text = text .. " - " .. tostring( col ) .. "[" .. tostring( val ) .. "]"
			end
			Msg( text .. "\n" )
		end
	end
	sql.Query( "DROP TABLE TestDB" )
	
	Msg( "END DB TEST\n----------\n" )
end

end